<?php

class Core_Frontend_Module_Subject_Latest extends Dcmp_Module_Base {

    protected $name = 'subject_latest';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form, $parameters = array()) {
        $categories = App::getModel('subject/collection_category')->load();
        $form->addComboTreeField($this->__('subject.label.category'),'category_id','module[parameters][category_id]')
                ->bindData($categories->toArray(), 0, array('valueKey' => 'id', 'textKey' => 'name', 'parentKey' => 'parent_id', 'childKey' => 'id'))
                ->setDefaultOption('', '--');
        $form->addBooleanField($this->__('share.label.is_show_index'), 'is_show_index', 'module[parameters][is_show_index]')
                ->setYesOption($this->__('status.boolean.1'))
                ->setNoOption($this->__('status.boolean.0'));
        $form->addSelectField($this->__('article.label.condition'),'sort','module[parameters][sort]')
                ->setOption($this->__('article.label.clickcount'),'clickcount')
                ->setOption($this->__('article.label.modified_time'),'modifiedtime')
                ->setOption($this->__('article.label.hot'), 'hot')
                ->setOption($this->__('article.label.new'), 'new')
                ->setOption($this->__('article.label.top'), 'top');
        $form->addTextBoxField($this->__('share.label.item_count'),'item_count','module[parameters][item_count]')
             ->addRequiredRule()
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_count'),'image_count','module[parameters][image_count]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_count'),'introduction_count','module[parameters][introduction_count]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_size'),'introduction_size','module[parameters][introduction_size]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_width'),'image_width','module[parameters][image_width]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_height'),'image_height','module[parameters][image_height]')
             ->setValue(0)
             ->addDigitsRule();
        $form->createTab('module_option', $this->__('design.label.module_parameter'),
                array('field_category_id', 'field_sort', 'field_is_show_index', 'field_item_count', 'field_introduction_count',
                      'field_introduction_size', 'field_image_count', 'field_image_width', 'field_image_height'));
    }

    public function render() {
        $obj = new Kd_Object($this->getParameters());
        $subjects = App::getModel('subject/collection_content')
                        ->resetSelect()
                        ->fields('a.id', 'a.name', 'a.introduction','a.picture')
                        ->sortBy($obj->getSort(),'desc')
                        ->filterBy('status','published')
                        ->limit($obj->getItemCount(),0);
        if($obj->getCategory()){
            $subjects->filterByCategoryId($obj->getCategoryId());
        }
        $subjects->load();
        if(count($subjects)==0) return '';
        $nameStartIndex = 0;
        $maxRecords = count($subjects);
        if($obj->getImageCount()){
            $obj->setIsShowImage(TRUE);
            $obj->setStartImageIndex(0);
            $obj->setEndImageIndex(min($maxRecords, $obj->getImageCount()));
            $nameStartIndex = max($obj->getEndImageIndex(), $nameStartIndex);
        }
        if($obj->getIntroductionCount()){
            $obj->setIsShowIntroduction(TRUE);
            $obj->setStartIntroductionIndex(0);
            $obj->setEndIntroductionIndex(min($maxRecords, $obj->getIntroductionCount()));
            $nameStartIndex = max($obj->getEndIntroductionIndex(), $nameStartIndex);
        }
        $obj->setStartNameIndex($nameStartIndex);
        $obj->setEndNameIndex(min($maxRecords, $obj->getItemCount()));
        $tmpl = $this->getTemplate()->assignMulti(array('subjects' => $subjects, 'parameters'=>$obj));
        return $tmpl->render($this->getTemplatePath($obj->getTemplateName(), $obj->getTemplateCode()));
    }

}

